﻿/*
 * (c) Stefan Bojarovski 2010
 * */
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASTBuilder
{
    class IdentifierNode : NodesCollection, IArithmeticNode
    {
        private LexemeType lexType = LexemeType.IDENTIFIER;
        private String Name;
        private IdentifierType idType;
        private bool UMinus;

        public IdentifierNode(String someName, IdentifierType someIType)
        {
            Name = someName;
            idType = someIType;
        }
        public LexemeType getLexemeType()
        {
            return lexType;
        }
        public void setNegative()
        {
            UMinus = true;
        }
        public String getName()
        {
            return Name;
        }
        public void printNode(String parentLevel, StreamWriter someStream)
        {
            someStream.WriteLine(parentLevel + " " + "Identifier: " + idType.ToString() + " " + Name);
        }
        public List<Command> evalNode(ref Direction next,
                                        ref Dictionary<String, Pair<IdentifierType, object>> Environment)
        {
            Command result = null;
            object Value;

            if (Environment.ContainsKey(Name))
            {
                IdentifierType identifierType = Environment[Name].getLeft();

                switch (identifierType)
                {
                    case IdentifierType.LABEL:
                        result = new Command(CommandType.FLOWCONTROLL, Name, null);
                        break;
                    case IdentifierType.BOOL:
                    case IdentifierType.INT:
                        Value = Environment[Name].getRight();
                        if (Value != null)
                        {
                            if (UMinus) { Value = (int)Value * (-1); }
                            result = new Command(CommandType.EVALUABLE, Name, Value);
                        }
                        else
                        {
                            result = new Command(CommandType.ERRORNULLARGUMENT, Name, null);
                        }
                        break;
                    /*case IdentifierType.EXPRESSION:
                        ITreeNode Subs = (ITreeNode)Environment[Name].getRight();
                        Command resultSubs = Subs.evalNode(ref next, ref Environment)[0];
                        object evaluated = resultSubs.getIdValue();

                        if (evaluated is bool)
                        {
                            Environment[Name] = new Pair<IdentifierType, object>(IdentifierType.BOOL, evaluated);
                        }
                        else if (evaluated is int)
                        {
                            Environment[Name] = new Pair<IdentifierType, object>(IdentifierType.INT, evaluated);
                        }
                        result = new Command(CommandType.ENVIRONMENTMODIFICATION, Name, evaluated);
                        break;*/
                    case IdentifierType.UNKNOWN:
                        result = new Command(CommandType.ERRORUNDECLAREDID, Name, null);
                        break;
                }
            }
            else
            {
                result = new Command(CommandType.ERRORNOSUCHID, Name, null);
            }

            List<Command> idResult = new List<Command>();
            idResult.Add(result);
            return idResult;
        }        
    }
}
